<?php

namespace app\admin\controller\sta\batch;

use app\admin\model\Depot;
use app\admin\model\Factory;
use app\admin\model\Pinxi;
use app\common\controller\Backend;
use think\Db;
use think\exception\DbException;
use think\response\Json;

/**
 * 生物资产收发存汇总管理
 *
 * @icon fa fa-circle-o
 */
class Bios extends Backend
{

    /**
     * Bios模型对象
     * @var \app\admin\model\sta\batch\Bios
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\sta\batch\Bios;
        $this->view->assign("statusList", $this->model->getStatusList());
    }


    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //   全部批次为行,  条件是:   批次有出栏

//  TODO:  功能完成后, 需要改为: 查询完成上月的数据后,记录存到表 xxx, 并锁定数据不可修改
//  TODO:  查询条件:  月份,当月可修改;  (可能还会按厂区拆分)

        //   @以全部库求汇总, 再处理数组,
        $storem1 = new \app\admin\model\Storem1 ;
        //  计算月份      *
        //   todo   需要改成可选月份
        //   查询本月数据是随动的,单条记录不可修改.  查询上月数据是已存到表 sta_material 的, 查询当月是即时的
        //   todo    $newdate  需要做成用户选择
        $newdate = date("Y-02-01");          // 月初       todo
        $month = date("Y-02");          // 月初       todo
        $newdate_31 = date("Y-m-d", strtotime("+1 month", strtotime($newdate) )  );    // 月底
        // 汇总月份
        $this->view->assign("mark", date("ym", strtotime($newdate) )."月" );

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        // 关联表查询
//        $pinxiList = Pinxi::getSelectList();
//        $depotList = Depot::getList();
//        $factoryList = Factory::getList();
//

//print_r($where);die;
        #  数据行  1
//        $list = $storem1
//            ->where($where)
//            ->field("*")
//            ->order("id ASC")
//            ->paginate($limit);
        #  2
//        $list = $storem1->alias("m1")
//            ->where($where)
//            ->join("storepro sp", "sp.storem1_id = m1.id", "right" )
//            ->join("batch b", "b.id = sp.batch_id", "right" )
//            ->field("m1.*, sp.id as sp_id, b.pinxi_id ")
//            ->order("m1.factory_id ASC")
//            ->paginate($limit);
        #  3
//        $list = Db::name("storepro")->alias("sp")
//            //->join("storepro sp", "sp.storem1_id = m1.id", "right" )
//            ->join("batch b", "b.id = sp.batch_id", "left" )
//            ->join("pinxi p", "p.id = b.pinxi_id", "left" )
//            ->field("sp.* , p.daici as gene_id, b.pinxi_id ")
//            ->where("DATE_FORMAT(sp.createtime,'%Y-%m')='{$month}' ")
//            //->order("sp.factory_id ASC")
//            ->paginate($limit);
        # 4
//        $factory = Factory::where("status",1)->select();
//print_r( Collection($factory)->toArray() );  die;
        $list = Db::name("storepro")->alias("sp")
            //->join("storepro sp", "sp.storem1_id = m1.id", "right" )
            ->join("batch b", "b.id = sp.batch_id", "left" )
            // ->join("pinxi p", "p.id = b.pinxi_id", "left" )
            ->field("sp.* , b.pinxi_id ")       //  p.daici as gene_id,
            ->where("DATE_FORMAT(sp.createtime,'%Y-%m')='{$month}' ")
            //->order("sp.factory_id ASC")
            ->paginate($limit);


//        $list = Db::name("batch")
            //->join("storepro sp", "sp.storem1_id = m1.id", "right" )
//            ->join("batch b", "b.id = sp.batch_id", "left" )
//            ->join("pinxi p", "p.id = b.pinxi_id", "left" )
//            ->field("sp.* , p.daici as gene_id, b.pinxi_id ")
//            ->where("DATE_FORMAT(sp.createtime,'%Y-%m')='{$month}' ")
            //->order("sp.factory_id ASC")
//            ->group("pinxi_id")
//            ->select();

//echo Db::getLastSql();
//print_r( Collection($list->items())->toArray() );  die;
//print_r( Collection($list)->toArray() );  die;

        // TODO:  如果计算合计,  那么要按当前筛选条件, 进行`不分页` 计算
//print_r($list);die;
        // TODO:  采集对应数据

        foreach($list as &$val){
            # 即时数据
            $cur = Db::name("storepro")
                ->where("storem1_id", $val['id'])
                ->where("cal", "销售")
                ->field("*, sum(num) as sum_num ")
                ->find();
//echo Db::getLastSql();die;

$val['a1'] = 12;                 //  a1  '月初存栏',
$val['a2'] = 0;                 //  a2  '单价',
$val['a3'] = 0;                 //  a3  '金额',

$val['b1'] = 0;                 //  b1  '转为生长',
$val['b2'] = 0;                 //  b2  '种兔自繁',
$val['b3'] = 0;                 //  b3  '转入数 ',
$val['b4'] = 0;                 //  b4  '单价',
$val['b5'] = 0;                 //  b5  '金额',

$val['c1'] = 0;                 //  c1  '转出数',
$val['c2'] = 0;                 //  c2  '单价',
$val['c3'] = 0;                 //  c3  '金额',

$val['d1'] = 0;                 //  d1  '淘汰数',
$val['d2'] = 0;                 //  d2  '单价',
$val['d3'] = 0;                 //  d3  '金额',

$val['e1'] = 0;                 //  e1  '死亡数',
$val['e2'] = 0;                 //  e2  '单价',
$val['e3'] = 0;                 //  e3  '金额',

$val['f1'] = $cur['sum_num'];   //  f1  '销售数',
$val['f2'] = 0;                 //  f2  '单价',
$val['f3'] = $cur['sum_num'] * $val['f2'];                 //  f3  '金额',

$val['g1'] = 0;                 //  g1  '月末存栏',
$val['g2'] = 0;                 //  g2  '单价',
$val['g3'] = 0;                 //  g3  '金额',


        }

        $list = $this->getXXX($list);
//        print_r($list);die;
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }



}
